<?php
/**
 * @file tags.php
 * @author CaoQiCheng
 * @date 2017年2月8日 下午17:05:11
 * @brief 
 */
namespace haibao\jiepai\business;

use haibao\jiepai\data\AppVersions as AppVersionsData;
use haibao\jiepai\model\data\app\CmsAppVersions as CmsAppVersionsModel;
use haibao\jiepai\model\filter\app\CmsAppVersionsPictures as CmsAppVersionsPicturesFilter;
use haibao\jiepai\model\data\app\CmsAppVersionsPictures as CmsAppVersionsPicturesModel;
use haibao\frame\data\query\Condition;
use haibao\jiepai\data\Fashionista as FashionistaData;
use haibao\jiepai\data\Article as ArticleData;
use haibao\jiepai\data\Message as MessageData;

class Common extends \haibao\jiepai\business\Base{	
	
	/**
	 * 状态码
	 */
	public static $phoneOS = array(
			CmsAppVersionsModel::PHONEOS_APPLE => '苹果',
			CmsAppVersionsModel::PHONEOS_ANDROID => '安卓',
			CmsAppVersionsModel::PHONEOS_DOUBLE => '苹果/安卓'
	);

	/**
	 * 状态码
	 */
	public static $updateVersion = array(
			CmsAppVersionsModel::STATUS_ADVICE => '建议升级',
			CmsAppVersionsModel::STATUS_FORCE => '强制升级',
	);
	
	public static function GetAPPVersionUpdate($phoneOsType){
		$data = new AppVersionsData();
		return $data->GetAPPVersionUpdate($phoneOsType);
	}
	
	public static function GetAppForceUpdateVersion($phoneOsType){
		$data = new AppVersionsData();
		return $data->GetAppForceUpdateVersion($phoneOsType);
	}
	
	public static function GetAppVersionUpdatePictures($versionId){
		$list = array();
		$data = new AppVersionsData();
		$pictureIds = $pictureUrl = array();
		$appVersionsPictures = new CmsAppVersionsPicturesFilter();
		$appVersionsPictures->select(array(
				$appVersionsPictures::CONDITION_FIELD_NAME_ID,
				$appVersionsPictures::CONDITION_FIELD_NAME_FILEID
		));
		$appVersionsPictures->where($appVersionsPictures::CONDITION_FIELD_NAME_VERSIONID, Condition::CONDITION_EQUAL, $versionId);
		$appVersionsPictures->where($appVersionsPictures::CONDITION_FIELD_NAME_FLAG, Condition::CONDITION_EQUAL, CmsAppVersionsPicturesModel::FLAG_IS);
		$appPictureData = $data->getAll($appVersionsPictures);
		foreach ($appPictureData as $key=>$val){
			$pictureIds[]['FileID'] =  $val->FileID;
		}

		$pictureUrl = $data->mergeDataAndImage($pictureIds,'FileID');

		return $pictureUrl;
	}
	
	public static function AppSearchFashionista($keyword ,$type='all'){
		$searchList = $avatarImage =  array();
		$data = new FashionistaData();
		$selArr = array(
			\haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
			\haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_CN,
			\haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_EN,
		);
		
		$result = \haibao\jiepai\business\Fashionista::searchCelebrityByName($selArr, $keyword); 
		
		if($result){
			$i = 0;
			foreach ($result as $key=>$model){
					$searchList[$i]['FashionistaID'] = $model->Id;
					$searchList[$i]['NameFr'] = $model->NameCn;
					$searchList[$i]['Name'] = $model->NameEn;
                    $picUrl = $model->ImageSrc;
                    if(!empty($picUrl)){
                        if (strpos($picUrl, '/jiepai-1/') === false) {
                            $picUrl = '/jiepai-1/' . ltrim($picUrl, '/');
                        }
                        $picUrl =  \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\jiepai\Config::CDN_URL_IMAGE).ltrim($picUrl, '/');
                    }
					$searchList[$i]['Avatar'] = $picUrl;
					$i++;
			}	
		}
		return $searchList;
	}
	
	/**
	 * 插入一个操作历史
	 */
	public static function InsertIntoSearchHistory($keyword,$userId){
		$data = new FashionistaData();
		return $data->InsertIntoSearchHistory($keyword,$userId);
	}
	
	/**
	 * 查询文章
	 */
	public static function SearchArticle($keyword,$cursor,$version){
		$articleImage = array();
		$data = new ArticleData();
		$result = $data->SearchArticle($keyword,$cursor,$version);
		$count = $result['count'];
		unset($result['count']);
		$pictureUrl = $data->mergeDataAndImage($result,'ContentInfo');
		foreach ($pictureUrl as $key=>$model){
			$articleImage[$model['ContentInfo']] = $model;
		}
		
		foreach ($result as $key=>$model){
			if(isset($articleImage[$model['ContentInfo']])){
				$result[$key]['ContentInfo']	= $articleImage[$model['ContentInfo']];
			}
		}
		$result['count'] = $count;
		return $result;
	}
	
	public static function GetWidgetArticles(){
		$data = new ArticleData();
		$result = $data->GetWidgetArticles();
		$result['mixedArticles'] =  $data->mergeDataAndImage($result['mixedArticles'],'Picture');
		$result['pictorialArticles'] =  $data->mergeDataAndImage($result['pictorialArticles'],'Picture');
		return $result;
	}
	
	public static function ClearVisitorByDeviceID($deviceId){
		$data = new ArticleData();
		return $data->ClearVisitorByDeviceID($deviceId);
	}
	
	public static function CreateVisitor($modelArr){
		$data = new ArticleData();
		return $data->CreateVisitor($modelArr);
	}
	
	public static function pushWelcomeMsg($userId, $visitorId){
		if (!$userId && !$visitorId) return;
		$data = new MessageData();
		return $data->pushWelcomeMsg($userId,$visitorId);
	}
}